/**
 * Cho phep cau hinh ket noi co so du lieu
 */
package view;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import model.Constant;
import model.database.ConnectDB;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * @author heroandtn3
 * @date Oct 23, 2012
 */
public class ConfigPanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final String KEY = "ConfigPanel";
	private MainPanel mainPanel;
	
	private JTextField tfHost;
	private JTextField tfPort;
	private JTextField tfUser;
	private JLabel lbPass;
	private JLabel lbBg;
	private JLabel lbNotice;
	private JLabel lbBack;
	private JLabel lbReset;
	private JLabel lbConnect;
	private JLabel lbPort;
	private JPasswordField tfPass;
	private JLabel lbDatabase;
	private JTextField tfDatabase;
	private JLabel lbName;
	private JLabel lbHost;
	
	
	public ConfigPanel(MainPanel mainPanel) {
		this.mainPanel = mainPanel;
		 File f = new File("config.xml");
	        if (f.exists()) {
	            ConnectDB.readConfig();
	        }
		initGUI();
	}

	private void initGUI() {
		try {
			{
				this.setLayout(null);
				this.setPreferredSize(new java.awt.Dimension(900, 680));
				this.setBackground(new java.awt.Color(0,138,0));
				{
					tfHost = new JTextField();
					this.add(tfHost);
					tfHost.setText(ConnectDB.getHost());
					tfHost.setBounds(496, 221, 126, 20);
					tfHost.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
				}
				{
					tfPort = new JTextField();
					this.add(tfPort);
					tfPort.setText(ConnectDB.getPort());
					tfPort.setBounds(759, 217, 70, 22);
					tfPort.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
				}
				{
					tfUser = new JTextField();
					this.add(tfUser);
					tfUser.setText(ConnectDB.getUser());
					tfUser.setBounds(496, 268, 126, 22);
					tfUser.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
				}
				{
					lbHost = new JLabel();
					this.add(lbHost);
					lbHost.setText("Host");
					lbHost.setBounds(361, 219, 100, 29);
					lbHost.setFont(new java.awt.Font("Segoe UI",0,16));
					lbHost.setForeground(new java.awt.Color(255,255,255));
				}
				{
					lbName = new JLabel();
					this.add(lbName);
					lbName.setText("Name");
					lbName.setBounds(361, 266, 100, 29);
					lbName.setFont(new java.awt.Font("Segoe UI",0,16));
					lbName.setForeground(new java.awt.Color(255,255,255));
				}
				{
					tfDatabase = new JTextField();
					this.add(tfDatabase);
					tfDatabase.setText(ConnectDB.getDatabase());
					tfDatabase.setBounds(496, 370, 126, 22);
					tfDatabase.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
				}
				{
					lbPass = new JLabel();
					this.add(lbPass);
					lbPass.setText("Password");
					lbPass.setBounds(361, 315, 100, 29);
					lbPass.setFont(new java.awt.Font("Segoe UI",0,16));
					lbPass.setForeground(new java.awt.Color(255,255,255));
				}
				{
					lbDatabase = new JLabel();
					this.add(lbDatabase);
					lbDatabase.setText("Database");
					lbDatabase.setBounds(361, 367, 100, 29);
					lbDatabase.setFont(new java.awt.Font("Segoe UI",0,16));
					lbDatabase.setForeground(new java.awt.Color(255,255,255));
				}
				{
					tfPass = new JPasswordField();
					this.add(tfPass);
					tfPass.setText(ConnectDB.getPassword());
					tfPass.setBounds(496, 318, 126, 22);
					tfPass.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
				}
				{
					lbPort = new JLabel();
					this.add(lbPort);
					lbPort.setText("Port");
					lbPort.setBounds(659, 215, 100, 29);
					lbPort.setFont(new java.awt.Font("Segoe UI",0,16));
					lbPort.setForeground(new java.awt.Color(255,255,255));
				}
				{
					lbConnect = new MyLabelButton(Constant.BUTTON_DIR + "ok", "Connect", false);
					this.add(lbConnect);
					lbConnect.setBounds(771, 262, 48, 48);
					lbConnect.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							//TODO add your code for lbConnect.mouseClicked
							writeConfig();
							ConnectDB.readConfig();
							if (ConnectDB.connect()) {
								lbNotice.setForeground(Color.GREEN);
								lbNotice.setText("Successful connection!");
							} else {
								lbNotice.setForeground(Color.RED);
								lbNotice.setText("Connection failure!");
							}
						}
					});
				}
				{
					lbReset = new MyLabelButton(Constant.BUTTON_DIR + "reset", "Reset", false);
					this.add(lbReset);
					lbReset.setBounds(771, 313, 48, 48);
					lbReset.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							//TODO add your code for lbReset.mouseClicked
							tfHost.setText("localhost");
							tfPort.setText("3306");
							tfDatabase.setText("oop");
							tfUser.setText("root");
							tfPass.setText("12345");
						}
					});
				}
				{
					lbBack = new MyLabelButton(Constant.BUTTON_DIR + "back", "Back to home", false);
					this.add(lbBack);
					lbBack.setBounds(771, 367, 48, 48);
					lbBack.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent evt) {
							//TODO add your code for lbBack.mouseClicked
							mainPanel.switchPanel("HomePanel");
						}
					});
				}
				{
					lbNotice = new JLabel();
					this.add(lbNotice);
					lbNotice.setBounds(496, 441, 333, 58);
				}
				{
					lbBg = new JLabel(new ImageIcon(Constant.BG_DIR + "connection.png"));
					this.add(lbBg);
					lbBg.setBounds(26, 215, 244, 245);
				}
			}
		} catch(Exception e) {
			//e.printStackTrace();
		}
	} // ket thuc phuong thuc initGUI()
	
	/**
	 *  Ham de viet du lieu ra XML
	 *  Nguon: suu tam
	 */
    private void writeConfig() {
        try {
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

            // root element
            Document doc = docBuilder.newDocument();
            Element rootElement = doc.createElement("connection");
            doc.appendChild(rootElement);

            // child elements
            Element eHostname = doc.createElement("hostname");
            eHostname.appendChild(doc.createTextNode(tfHost.getText().trim()));
            rootElement.appendChild(eHostname);

            Element ePort = doc.createElement("port");
            ePort.appendChild(doc.createTextNode(tfPort.getText().trim()));
            rootElement.appendChild(ePort);

            Element eUsername = doc.createElement("username");
            eUsername.appendChild(doc.createTextNode(tfUser.getText().trim()));
            rootElement.appendChild(eUsername);

            Element ePassword = doc.createElement("password");
            ePassword.appendChild(doc.createTextNode(String.valueOf(tfPass.getPassword())));
            rootElement.appendChild(ePassword);

            Element eDatabase = doc.createElement("database");
            eDatabase.appendChild(doc.createTextNode(tfDatabase.getText().trim()));
            rootElement.appendChild(eDatabase);

            // Viet noi dung ra xml
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(new File("config.xml"));

            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3");
            transformer.transform(source, result);

        } catch (TransformerException ex) {
            //ex.printStackTrace();
        } catch (ParserConfigurationException ex) {
            //ex.printStackTrace();
        }

    }

}
